package org.rsbot.script.methods;

import java.util.ArrayList;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItem;

public class Store extends MethodProvider
{
  public static final int INTERFACE_STORE = 620;
  public static final int INTERFACE_STORE_BUTTON_CLOSE = 18;
  public static final int INTERFACE_STORE_ITEMS = 25;

  Store(MethodContext paramMethodContext)
  {
    super(paramMethodContext);
  }

  public boolean buy(int paramInt1, int paramInt2)
  {
    if (paramInt2 < 0)
      return false;
    if (!isOpen())
      return false;
    int i = this.methods.inventory.getCount(true);
    RSItem localRSItem = getItem(paramInt1);
    if (localRSItem != null) {
      if (paramInt2 >= 500) {
        if (localRSItem.doAction("Buy 500")) {
          sleep(random(500, 700));
          return buy(paramInt1, paramInt2 - 500);
        }
        return false;
      }if ((paramInt2 >= 50) && (paramInt2 < 500)) {
        if (localRSItem.doAction("Buy 50")) {
          sleep(random(500, 700));
          return buy(paramInt1, paramInt2 - 50);
        }
        return false;
      }if ((paramInt2 >= 10) && (paramInt2 < 50)) {
        if (localRSItem.doAction("Buy 10")) {
          sleep(random(500, 700));
          return buy(paramInt1, paramInt2 - 10);
        }
        return false;
      }if ((paramInt2 >= 5) && (paramInt2 < 10)) {
        if (localRSItem.doAction("Buy 5")) {
          sleep(random(500, 700));
          return buy(paramInt1, paramInt2 - 5);
        }
        return false;
      }if ((paramInt2 >= 1) && (paramInt2 < 5)) {
        if (localRSItem.doAction("Buy 1")) {
          sleep(random(500, 700));
          return buy(paramInt1, paramInt2 - 1);
        }
        return false;
      }

      return this.methods.inventory.getCount(true) > i;
    }

    return false;
  }

  public boolean close()
  {
    if (!isOpen()) {
      return true;
    }
    if (this.methods.interfaces.getComponent(620, 18).doClick()) {
      sleep(random(500, 600));
      return !isOpen();
    }
    return false;
  }

  public RSInterface getInterface()
  {
    return this.methods.interfaces.get(620);
  }

  public RSItem getItemAt(int paramInt)
  {
    RSItem[] arrayOfRSItem1 = getItems();
    if (arrayOfRSItem1 != null) {
      for (RSItem localRSItem : arrayOfRSItem1) {
        if (localRSItem.getComponent().getComponentIndex() == paramInt) {
          return localRSItem;
        }
      }
    }
    return null;
  }

  public RSItem getItem(int paramInt)
  {
    RSItem[] arrayOfRSItem1 = getItems();
    if (arrayOfRSItem1 != null) {
      for (RSItem localRSItem : arrayOfRSItem1) {
        if (localRSItem.getID() == paramInt) {
          return localRSItem;
        }
      }
    }
    return null;
  }

  public RSItem[] getItems()
  {
    if ((getInterface() == null) || (getInterface().getComponent(25) == null)) {
      return null;
    }
    ArrayList localArrayList = new ArrayList();
    RSComponent[] arrayOfRSComponent = getInterface().getComponent(25).getComponents();

    for (int i = 0; i < arrayOfRSComponent.length; i++)
    {
      if ((arrayOfRSComponent[i] != null) && (arrayOfRSComponent[i].getComponentID() != -1)) {
        localArrayList.add(new RSItem(this.methods, arrayOfRSComponent[i]));
      }
    }
    return (RSItem[])localArrayList.toArray(new RSItem[localArrayList.size()]);
  }

  public boolean isOpen()
  {
    return getInterface().isValid();
  }
}